package com.example.haojiutao.controller.admin;

import com.example.haojiutao.common.Result;
import com.example.haojiutao.model.domain.Goods;
import com.example.haojiutao.service.GoodsService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Description: 商品的控制层
 * @Author: ma_lihong
 * @CreateTime: 2024/1/16 16:53
 */
@Slf4j
@RestController
public class GoodsAdminController {


    @Autowired
    private GoodsService goodsService;
    /*
     * @Description: 添加闲置
     * @Author: ma_lihong
     * @CreateTime: 2024/1/16 19:38
     * @param: goods
     * @return: com.example.haojiutao.common.Result
     */
    @PostMapping("/admin/goods") //等于@RequestMapping(method =POST ,value ="/path" )
    public Result insertGoods(@RequestBody Goods goods){

        log.info("添加商品：{}",goods);

        return goodsService.insertGoods(goods);
    }
    /*
     * @Description:更新商品价格，图片，详情，品牌
     * @Author: ma_lihong
     * @CreateTime: 2024/1/16 19:39
     * @param: goods
     * @return: com.example.haojiutao.common.Result
     */
    @PutMapping("/admin/goods/{goodsId}")
    public Result updateGoods(@RequestBody@PathVariable int goodsId){
        Goods goods = new Goods();
        log.info("更新商品信息:goods{}",goods);
        goods.setGoodsId(goodsId);
        return goodsService.updateGoods(goods);

    }

    /*
     * @Description:根据商品名称搜索商品，显示商品列表
     * @Author: ma_lihong
     * @CreateTime: 2024/1/17 21:04
     * @return: com.example.haojiutao.common.Result
     */
    @DeleteMapping("/admin/goods/delete/{goodsId}")
    public Result deleteGoods(@PathVariable int goodsId){

        return goodsService.deleteById(goodsId);

    }
    /*
     * @Description: 下架商品、删除商品
     * @Author: ma_lihong
     * @CreateTime: 2024/1/17 20:47
     * @param: goodId
     * @return: com.example.haojiutao.common.Result
     */
    @GetMapping("/admin/goods/list")
    public Result selectByName(Goods goods){
        log.info("根据商品名查找");

        System.out.println("-----------------"+goods);
        List<Goods> good = goodsService.selectByName(goods);
        if(good!=null){
            for (Goods goods1:good){
                System.out.println("################"+goods1.toString());
            }
        }else
        {
            System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!");
        }
        PageInfo<Goods> pageInfo = new PageInfo<>(good);
        return Result.success("查询成功",pageInfo);

    }
    /*
     * @Description: 查看单个物品，根据id查询,查看商品详情
     * @Author: ma_lihong
     * @CreateTime: 2024/1/18  14:21
     * @param: goods
     * @return: com.example.haojiutao.common.Result
     */
    @GetMapping("/admin/goods/{goodsId}")
    public Result selectById(@PathVariable int goodsId){
        Goods goods = new Goods();
        log.info("查看单个商品");
        Goods goods1 = goodsService.selectById(goodsId);
        return Result.success("查询成功！",goods);
    }

}